<?php
require '../include/library.inc.php';
require '../include/admin_login.inc.php';
require '../include/paging.inc.php';

$sql = "SELECT
			o.order_id,
			date_format(o.creation_datetime, \"%Y/%m/%d\") AS creation_date,
			s.name AS school_name,
			u.user_id,
			u.first_name,
			u.last_name,
			u.email,
			os.name AS order_status
		FROM
		  	orders AS o,
			schools AS s,
			users AS u,
			order_status AS os
		WHERE
			o.school_id = s.school_id
		AND
			o.user_id = u.user_id
		AND
			o.order_status_id = os.order_status_id
		  ";
		  
if (!isset($_GET['sort'])) $_GET['sort'] = 1;
if (!isset($_GET['sortBy'])) $_GET['sortBy'] = 1;

$paging = new Paging($sql, $_GET['sortBy'], $_GET['sort'], array(1=>"Order ID", 3=>"School Name"), 30);
$result = $paging->query();
	
	head_admin("Orders");
?>
		
			<?php if ($_SESSION['message']) { ?>
			<div class="message green">
				<?=$_SESSION['message']?>
			</div>
			<?php unset($_SESSION['message']); } ?>
		
			
            
            <div class="topbuttons">
            	 Order by: <?=$paging->displayNext()?> <?=$paging->displayNext()?>
            </div>
            
            <h3>Orders (<?=$paging->count()?>)</h3>
            
            <table id="listing" width="100%" cellpadding="0" cellspacing="0">
			<thead>
				<tr>
                    <td>Order ID</td>
                    <td>School</td>
                    <td>User</td>
                    <td>Status</td>
                    <td>Options</td>
				</tr>
			</thead>
			<tbody>
			<?php
	  
			  $i=1;
			  while ($row = mysql_fetch_object($result)) {				  
			?>
                <tr class="<?=(($i%2)?"none":"zebra")?>">
                    
                    <td>
                        <a href="order_edit.php?order_id=<?=$row->order_id?>" title="Edit"><?=number_pad($row->order_id,5)?></a>
                        from <?=$row->creation_date?>
                    </td>
                    
                    <td>
                    	<?=$row->school_name?>
                    </td>
                    
                    <td>
                    <a href="user_edit.php?user_id=<?=$row->user_id?>">
						<?=$row->first_name?> <?=$row->last_name?>, <?=$row->email?>
                    </a>
                    </td>
                            
                    <td class="icons">
                    	<?=$row->order_status?>
                    </td>
                                
                    <td class="icons">
                                            
                        <a href="order_edit.php?order_id=<?=$row->order_id?>" class="edit" title="Edit">Edit</a>
                        
                    </td>
                    
                </tr>
                    
			 <?php
                  $i++;    
                } 
             ?>	
		 		  
			</tbody>
		</table>
        
        <div class="pagingwrap"><div class="paging">
			<?=$paging->display()?>
        </div></div>
<?php
	foot_admin(); 
?>